package com.jz.community.moduleshopping.integralGoods.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jz.community.basecomm.base.BaseMvpFragment;
import com.jz.community.basecomm.mvp.BaseMvpPresenter;
import com.jz.community.basecomm.routerUtils.RouterCommonUtils;
import com.jz.community.basecomm.routerUtils.RouterIntentConstant;
import com.jz.community.basecomm.task.ITaskCallbackListener;
import com.jz.community.basecomm.utils.Preconditions;
import com.jz.community.basecomm.utils.SHelper;
import com.jz.community.moduleshopping.R;
import com.jz.community.moduleshopping.R2;
import com.jz.community.moduleshopping.integralGoods.adapter.IntegralGoodsAdapter;
import com.jz.community.moduleshopping.integralGoods.bean.IntegralGoodsInfo;
import com.jz.community.moduleshopping.integralGoods.task.GetIntegralGoodsTask;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by Tony.Fan on 2018/7/2 17:49
 */
public class IntegralGoodsFragment extends BaseMvpFragment implements BaseQuickAdapter.RequestLoadMoreListener {

    @BindView(R2.id.integral_recyclerView)
    RecyclerView integralRecyclerView;
    @BindView(R2.id.no_data_empty)
    LinearLayout no_data_empty;
    @BindView(R2.id.emptyTxt)
    TextView emptyTxt;
    private int page = 0;
    private IntegralGoodsAdapter integralGoodsAdapter;
    private int disPriceType = 1;

    public void setDisPriceType(int disPriceType) {
        this.disPriceType = disPriceType;
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.module_shopping_integral_home_goods_item_layout;
    }

    public static IntegralGoodsFragment newInstance(int disType) {
        IntegralGoodsFragment integralGoodsFragment = new IntegralGoodsFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("disType", disType);
        integralGoodsFragment.setArguments(bundle);
        return integralGoodsFragment;
    }

    @Override
    protected void initView() {
        super.initView();
        disPriceType = getArguments().getInt("disType");
    }

    @Override
    protected void initLazy(@Nullable Bundle savedInstanceState) {
        super.initLazy(savedInstanceState);
        getIntegralData();
    }

    @Override
    protected void initDatas() {
        super.initDatas();
        handleBindAdapter();
    }

    private void getIntegralData() {
        new GetIntegralGoodsTask(getActivity(), new ITaskCallbackListener() {
            @Override
            public void doTaskComplete(Object o) {
                IntegralGoodsInfo integralGoodsInfo = (IntegralGoodsInfo) o;
                if (!Preconditions.isNullOrEmpty(integralGoodsInfo)) {
                    SHelper.gone(no_data_empty);
                    setData(false, integralGoodsInfo.get_embedded().getContent(), integralGoodsInfo.getPage().getTotalPages());
                } else {
                    SHelper.vis(no_data_empty);
                    emptyTxt.setText("暂无兑换商品！");
                }
            }
        }).execute(page + "", disPriceType + "");
    }

    private void handleBindAdapter() {
        LinearLayoutManager main_layoutManager = new LinearLayoutManager(getActivity());
        integralRecyclerView.setLayoutManager(main_layoutManager);
        integralGoodsAdapter = new IntegralGoodsAdapter(new ArrayList<>());
        integralGoodsAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);//加载动画
        integralGoodsAdapter.setOnLoadMoreListener(this, integralRecyclerView);
        integralGoodsAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            if (view.getId() == R.id.integral_btn) {
                RouterCommonUtils.startParameterActivity(RouterIntentConstant.MODULE_SHOPPING_GOODS_DETAIL, "url", integralGoodsAdapter.getData().get(position).getLinks().getSelf().getHref());
            }
        });
        integralRecyclerView.setAdapter(integralGoodsAdapter);
    }

    private void setData(boolean isRefresh, List data, int totalPages) {

        page++;

        if (isRefresh) {
            integralGoodsAdapter.setNewData(data);
        } else {
            integralGoodsAdapter.addData(data);
        }

        if (totalPages <= page) {
            integralGoodsAdapter.loadMoreEnd();
        } else {
            integralGoodsAdapter.loadMoreComplete();
        }
    }

    @Override
    protected BaseMvpPresenter createPresenter() {
        return null;
    }

    @Override
    public void onLoadMoreRequested() {
        getIntegralData();
    }
}
